package cn.tinkling;

import org.eclipse.swt.SWT;
import org.eclipse.ui.console.ConsolePlugin;
import org.eclipse.ui.console.IConsole;
import org.eclipse.ui.console.IConsoleFactory;
import org.eclipse.ui.console.IConsoleManager;
import org.eclipse.ui.console.MessageConsole;
import org.eclipse.ui.console.MessageConsoleStream;
import org.eclipse.wb.swt.SWTResourceManager;

public class TinklingConsole implements IConsoleFactory {

	private static MessageConsole sConsole = new MessageConsole("Tinkling", null);

	@Override
	public void openConsole() {
		open();
	}

	public static void open() {
		MessageConsole tinkling = sConsole;
		IConsoleManager manager = ConsolePlugin.getDefault().getConsoleManager();
		IConsole[] existing = manager.getConsoles();
		for (IConsole iConsole : existing)
			if (tinkling == iConsole)
				return;

		manager.addConsoles(new IConsole[] { tinkling });
	}

	public static void close() {
		IConsoleManager manager = ConsolePlugin.getDefault().getConsoleManager();
		manager.removeConsoles(new IConsole[] { sConsole });
	}

	public static MessageConsole getConsole() {
		return sConsole;
	}

	public static MessageConsoleStream getPrinter() {
		return sConsole.newMessageStream();
	}

	public static void print(String tag, String message) {
		print(tag, message, false);
	}

	public static void print(String tag, String message, boolean activate) {
		MessageConsoleStream printer = sConsole.newMessageStream();
		printer.setActivateOnWrite(activate);
		printer.print("[" + tag + "] ");
		printer.println(message);
	}

	public static void printErr(String tag, String message, boolean activate) {
		MessageConsoleStream printer = sConsole.newMessageStream();
		printer.setColor(SWTResourceManager.getColor(SWT.COLOR_RED));
		printer.setActivateOnWrite(activate);
		printer.print("[" + tag + "][E] ");
		printer.println(message);
	}

	public static void printInfo(String tag, String message) {
		printInfo(tag, message, false);
	}

	public static void printInfo(String tag, String message, boolean activate) {
		MessageConsoleStream printer = sConsole.newMessageStream();
		printer.setColor(SWTResourceManager.getColor(SWT.COLOR_DARK_GREEN));
		printer.setActivateOnWrite(activate);
		printer.print("[" + tag + "][I] ");
		printer.println(message);
	}

}
